layui.use(['form','layer','table','laytpl','util'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table,
        util = layui.util;

    // 流程实例列表
    var tableIns = table.render({
        elem: '#processInstanceList',
        url : '/deploy/processInstanceList',
        cellMinWidth : 65,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "processInstanceListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'processDefinitionKey', title: '流程定义key', width:80, align:"center"},
            {field: 'processDefinitionName', title: '流程定义名', minWidth:100, align:"center"},
            {field: 'processDefinitionVersion', title: '版本', width:80, align:"center"},
            {field: 'startUserId', title: '发起人', minWidth:100, align:"center"},
            {field: 'startTime', title: '开始时间', minWidth:100, align:"center", templet:function (d) {
                return util.toDateString(d.startTime, "yyyy-MM-dd HH:mm:ss");
            }},
            {field: 'endTime', title: '结束时间', minWidth:100, align:"center", templet:function (d) {
                return d.endTime == null?"<span style='color:#FF5722'>未结束</span>":util.toDateString(d.endTime, "yyyy-MM-dd HH:mm:ss");
            }},
            {title: '操作', minWidth:175, templet:'#processInstanceListBar', fixed:"right", align:"center"}
        ]]
    });

    //列表操作
    table.on('tool(processInstanceList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'detail'){ // 详情
            detail(data);
        }else if(layEvent === 'showImg'){ //查看流程图
            showImg(data);
        }else if(layEvent === 'del'){ //删除
            layer.msg("暂未实现此功能");
        }
    });

    function detail(data){
        // 根据流程定义ID判断显示哪种审核表单
        var content;
        var title;
        if(data.processDefinitionKey == "qjlc") {
            content = "/page/common/qjlcDetail";
            title = "请假单详情";
        }
        if(data.processDefinitionKey == "sqjflc") {
            content = "/page/common/sqjflcDetail";
            title = "申请经费详情";
        }

        var index = layui.layer.open({
            title: title,
            type: 2,
            content: content,
            area: ['520px', '600px'],
            success: function (res) {
                var body = layui.layer.getChildFrame('body', index);
                if (data) {
                    body.find(".processInstanceId").val(data.processInstanceId);  // 流程实例ID
                    body.find(".businessKey").val(data.businessKey);  // 关联的业务单ID
                    form.render();
                }
            }
        })
    }

    // 展示流程图
    function showImg(data) {
        //如果流程实例已结束，则提示无法查看流程图
        if(data.endTime != null){
            layer.msg("该流程实例已结束");
            return;
        }
        var index = layui.layer.open({
            title: "查看流程图",
            type: 2,
            content: "/deploy/viewCurrentImageByBusinessKey?businessKey="+data.businessKey,
            area: ['780px', '560px'],
            success: function (res) {

            }
        })
    }
});